import { _decorator, animation, BoxCollider2D, Collider2D, Component, Contact2DType, find, instantiate, IPhysics2DContact, Node, Prefab, RigidBody2D, Vec2 } from 'cc';
import { ZombieState } from './Global';
import { Plant } from './Plant';
import { GameManager } from './manager/GameManager';
import { ZombieManager } from './manager/ZombieManager';
import { AudioManager } from './manager/AudioManager';
import { Config } from './manager/Config';
const { ccclass, property } = _decorator;

@ccclass('Zombie')
export class Zombie extends Component {

    @property(Number)
    moveSpeed: number = 0;//移动速度
    zombieState: ZombieState;//僵尸状态
    anim: animation.AnimationController;//动画控制器

    @property(Number)
    atk: number = 0;//攻击力
    @property(Number)
    atkSpeed: number = 0;//攻击频率
    private atkTimer: number = 0;//攻击计时器
    private deltaTime: number;
    private currentEatPlant: Plant | null = null;//当前正在吃的植物
    @property(Number)
    public HP: number = 100;//最大生命值
    @property(Number)
    private currentHP: number = 0;//当前生命值
    @property(Prefab)
    zombieHeadPrefab: Prefab = null;//僵尸头节点
    private haveHead: boolean = true;//是否拥有头节点

    start() {
        this.currentHP = this.HP;
        this.zombieState = ZombieState.Move;
        let collider = this.getComponent(Collider2D);
        this.anim = this.getComponent(animation.AnimationController);
        if (collider) {
            collider.on(Contact2DType.BEGIN_CONTACT, this.onBeginContact, this);
            collider.on(Contact2DType.END_CONTACT, this.onEndContact, this);
        }
    }
    update(deltaTime: number) {
        this.deltaTime = deltaTime;
        switch (this.zombieState) {
            case ZombieState.Move:
                this.move();
                break;
            case ZombieState.Eat:
                this.stopMove();
                this.eat();
                break;
            case ZombieState.Die:
                this.stopMove();
            case ZombieState.Pause:
                break;
        }

    }
    move() {
        this.node.getComponent(RigidBody2D).linearVelocity = new Vec2(this.moveSpeed * -1, 0);
    }
    stopMove() {
        this.node.getComponent(RigidBody2D).linearVelocity = new Vec2(0, 0);
    }
    eat() {
        this.atkTimer += this.deltaTime;
        console.log(this.atkTimer);
        if (this.atkTimer >= this.atkSpeed) {
            AudioManager.Instance.playClip(Config.eat,2);
            this.currentEatPlant?.takeDamage(this.atk);
            this.atkTimer = 0;
        }
    }
    dead() {
        if (this.zombieState == ZombieState.Die) return;
        this.stopMove();
        // 设置僵尸状态为死亡
        this.zombieState = ZombieState.Die;
        // 禁用碰撞组件
        this.node.getComponent(BoxCollider2D).enabled = false;
        ZombieManager.Instance.removeZombie(this);
        // 延迟2秒后销毁节点
        this.schedule(() => {
            this.node.destroy();
        }, 2);
    }
    onBeginContact(self: Collider2D, other: Collider2D, contact: IPhysics2DContact | null) {
        if (other.group == 1 << 1) {
            this.anim.setValue("isAttacking", true);
            this.transitionToEat();
            this.currentEatPlant = other.node.getComponent(Plant);
        } else if (other.group == 1 << 4) {
            GameManager.Instance.gameOver();
        }
    }
    onEndContact(self: Collider2D, other: Collider2D, contact: IPhysics2DContact | null) {
        if (other.group == 1 << 1) {
            this.anim.setValue("isAttacking", false);
            this.zombieState = ZombieState.Move;
            this.currentEatPlant = null;
        }
    }
    transitionToEat() {
        this.zombieState = ZombieState.Eat;
        this.atkTimer = 0;
    }
    transitionToPause() {
        this.zombieState = ZombieState.Pause;
        this.stopMove();
        this.anim.enabled = false;
    }
    takeDamage(damage: number) {
        // 如果当前生命值小于等于0，则直接返回
        if (this.currentHP <= 0) return;
        // 扣除生命值
        this.currentHP -= damage;
        // 计算生命值百分比
        let hpPercent = this.currentHP / this.HP;
        // 如果当前生命值小于等于0
        if (hpPercent < 0.5 && this.haveHead) {
            this.haveHead = false;
            let zombieHead = instantiate(this.zombieHeadPrefab);
            zombieHead.setParent(find("Canvas/Game"));
            zombieHead.setPosition(this.node.getPosition());
            // 延迟2秒后销毁节点
            this.schedule(() => {
                zombieHead.destroy();
            }, 2);
        }
        if (this.currentHP <= 0) {
            // 将当前生命值设为-1，表示已死亡
            this.currentHP = -1;
            // 调用死亡方法
            this.dead();
        }
        // 设置动画中HPPercent的值
        this.anim.setValue("HPPercent", hpPercent);
    }
}
